package com.xx.happychatboot.middleware.springsecurity.security;

import com.xx.happychatboot.serve.user.domain.entity.SysRole;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class UserDetail implements UserDetails {

    private Long id;
    private String username;
    private String password;
    private Boolean enabled;
    private Boolean accountNonExpired;
    private Boolean accountNonLocked;
    private Boolean credentialsNonExpired;
    private List<SysRole> roles = new ArrayList<>();
    @Override
    public Collection<? extends GrantedAuthority>
    getAuthorities() {
        List<GrantedAuthority> grantedAuthorities = new
                ArrayList<>();
        roles.forEach(role -> grantedAuthorities.add(new
                SimpleGrantedAuthority(role.getRoleCode())));
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return password;
    }
    @Override
    public String getUsername() {
        return username;
    }
    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }
    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }
    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}
